package org.gruposp2p.dnie.client.ui.widget;

import org.gruposp2p.dnie.client.server.StatusData;
import org.gruposp2p.dnie.client.ui.panel.MainPanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.ScrollPanel;

public class GlassedDialog extends DialogBox implements ClickHandler {	

    private PopupPanel glass;
	private static GlassedDialog instance; 
    
    private GlassedDialog() {    	
        glass = new PopupPanel();
        glass.setStylePrimaryName("rx-glass"); 
    }

    public static GlassedDialog getInstance () {
    	if (instance == null) {
    		instance = new GlassedDialog();
    	}
    	return instance;
    }
    
    public void showErrorGettingResource (StatusData status) {
    	String errorMessage = MainPanel.constants.ConnectionErrorMessage() + "<br/><br/>" +
    		MainPanel.constants.statusCodeMsg() + " " + status.code + "<br/>" +
    		MainPanel.constants.statusDescriptionMsg() + " " + status.description + "<br/>";
		setText(MainPanel.constants.GlassedDialogCaption());
		FlexTable layout = new FlexTable(); 
		layout.clear();
        layout.setCellSpacing(6);
        layout.setWidth("300px");
        FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
        HTML html = new HTML(errorMessage);
        ScrollPanel scroller = new ScrollPanel(html);
        scroller.setSize("500px", "400px");
        layout.setWidget(0, 0, scroller);
        HorizontalPanel hPanel = new HorizontalPanel();
        hPanel.setSpacing(5);
        //layout.setWidget(0, 0, aulaApplication.getImages().loading().createImage());
        Button acceptButton = new Button(MainPanel.constants.acceptButtonText(), this);
        hPanel.add(acceptButton);
        layout.setWidget(1, 0, hPanel);
        cellFormatter.setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
        setWidget(layout);
        //To solve Firefox strange effect
        //widget.setFocus(true);
        glass.show();
        show();
        center();
    }
    
    public void showLoadingDataDialog () {
    	showLoadingObjectDialog (MainPanel.constants.LoadingDataMessage());
    }  
    
    private void showLoadingObjectDialog (String objectMessage) { 
        setText(MainPanel.constants.GlassedDialogCaption());
        FlexTable layout = new FlexTable();
        layout.clear();
        layout.setCellSpacing(6);
        layout.setWidth("250px");
        FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
        HTML html = new HTML(objectMessage);
        html.setStylePrimaryName("loadingMessage");
        layout.setWidget(0, 0, html);
        //HorizontalPanel hPanel = new HorizontalPanel();
        //hPanel.setSpacing(5);
        //layout.setWidget(1, 0, hPanel);
        //cellFormatter.setHorizontalAlignment(
        //		1, 0, HasHorizontalAlignment.ALIGN_RIGHT);
        //loadingImage = "<img src=\"" + GWT.getModuleBaseURL() + "images/loading.gif\">";
        setWidget(layout);
        //To solve Firefox strange effect
        //widget.setFocus(true);
        glass.show();
        show();
        center();
    }
    
    public void hideDialog () {
        glass.hide();
        hide();
    }
    
    @Override
    public void onClick(ClickEvent event) {
        glass.hide();
        hide();
    }
	
}